<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增实验请求')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-request-add">
        <h4 class="form-header h4">基本信息</h4>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">名称：</label>
            <div class="col-sm-8">
                <input name="reqName" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">描述：</label>
            <div class="col-sm-8">
                <input name="reqDes" class="form-control" type="text">
            </div>
        </div>
        <h4 class="form-header h4">项目选择</h4>
        <div class="form-group">
            <label class="col-sm-3 control-label">项目：</label>
            <div class="col-sm-8">
                <select name="proId" class="form-control selectpicker" data-none-selected-text="请选择项目">
                    <option th:each="project:${projects}" th:data-subtext="${project.proDes}"  th:value="${project.proId}" th:text="${project.proName}"></option>
                </select>
            </div>
        </div>

        <h4 class="form-header h4">Sample信息</h4>
        <div class="row">
            <div class="col-sm-12">
                <button type="button" class="btn btn-success btn-sm" onclick="addRow()"><i class="fa fa-plus"> 增加</i></button>
                <button type="button" class="btn btn-danger btn-sm" onclick="sub.delRow()"><i class="fa fa-minus"> 删除</i></button>
                <div class="col-sm-12 select-table table-striped">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>

    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<th:block th:include="include :: bootstrap-select-js" />
<script th:inline="javascript">
    var prefix = ctx + "system/request2";
    $("#form-request-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var data = $('#form-request-add').serializeArray();
            $.operate.save(prefix + "/add", data);
        }
    }

    // function getData(params) {
    //     var data = [];
    //     $.each(params, function(index, v){
    //         v.index = index;
    //         data.push(v);
    //     })
    //     return data;
    // }

    $(function() {
        var options = {
            // data: getData(sams),
            modalName: "服务",
            pagination: false,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            sidePagination: "client",
            columns: [{
                checkbox: true
            },
                {
                    field: 'samId',
                    title: 'id',
                    visible: false,
                },
                {
                    field: 'samName',
                    title: '样品名称',
                },
                {
                    field: 'samSlot',
                    title: 'slot',
                    align: 'center'
                },
                {
                    field: 'samDes',
                    title: '描述'
                },
                {
                    field: 'instName',
                    title: '仪器',
                    formatter: function (value, row, index) {
                        return  row.services[0].instrument.instName;
                    }
                },
                {
                    field: 'solvent',
                    title: '溶剂'
                },
                {
                    field: 'priority',
                    title: '优先',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(yesnoStatusDatas, value);
                    }
                },
                {
                    field: 'services',
                    title: '服务',
                    formatter: function (value, row, index) {
                        var services = [];
                        $.each(value,function (index, data) {
                            var s = JSON.stringify(data);
                            var parse = JSON.parse(s);
                            services.push('<a href="javascript:;"><i class="fa fa-adjust fa-lg"></i> '+parse.serName+'</a><br>')
                        })
                        return services.join('');
                    }
                },
                {
                    field: 'methods',
                    title: '方法',
                    formatter: function (value, row, index) {
                        var methods = [];
                        $.each(value,function (index, data) {
                            var s = JSON.stringify(data);
                            var parse = JSON.parse(s);
                            methods.push('<a href="javascript:;" ><i class="fa fa-adjust fa-lg"></i> '+parse.modName+'</a><br>')
                        })
                        return methods.join('');
                    }
                },
                {
                    field: 'quantity',
                    title: '样品量'
                },
                {
                    field: 'mFormula',
                    title: '分子式'
                },
                {
                    field: 'mWeight',
                    title: '分子量'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        var edit = '<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="editSample(\'' + index + '\')"><i class="fa fa-edit"></i>编辑</a> ';
                        actions.push(edit);
                        return actions.join('');
                    }
                }
            ]
        };
        $.table.init(options);
    });

    /* Add Sample*/
    function addRow() {
        var options = {
            title: 'Add Sample',
            url: prefix + "/addSam/"+reqId,
            callBack: doSubmit
        };
        $.modal.openOptions(options);

        function doSubmit(index, layero){
            var sample = layero.find("iframe")[0].contentWindow.getSample();
            // console.log(sample);
            if(sample == null){
                return false;
            }
            //获取选择的方法id集合
            var modIds = [];
            $.each(sample, function (index, value) {
                if(sample[index].name.startsWith("method")){
                    modIds.push(sample[index].value)
                }
            })
            var config = {
                url: prefix + "/getServices",
                type: "post",
                dataType: "json",
                data: {"modIds": modIds.toString()},
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍候...");
                },
                success: function(result) {
                    //回调事件
                    var count = $("#" + table.options.id).bootstrapTable('getData').length;
                    var row = {
                        index: $.table.serialNumber(count),
                        samName: sample[0].value,
                        samSlot: sample[1].value,
                        samDes: sample[2].value,
                        instName: "",
                        solvent: sample[6].value,
                        priority: sample[sample.length-1].value,
                        services: result.data.services,
                        methods: result.data.methods,
                        quantity: sample[3].value,
                        mFormula: sample[4].value,
                        mWeight: sample[5].value,
                    }
                    sub.addRow(row);
                    $.modal.closeLoading();
                }
            };
            $.ajax(config)
            $.modal.close(index);
        }
    }

</script>
</body>
</html>